package com.iamk.util;

import java.awt.image.BufferedImage;

public class ConvertColorSpace {
	public static int[][] convertRGBtoY(BufferedImage images) {
		int[][] channelY = new int[images.getHeight()][images.getWidth()];
		int[] temp = null;
		for (int i = 0; i < images.getHeight(); i++) {
			for (int j = 0; j < images.getWidth(); j++) {
				int k = images.getRGB(j, i);
				temp = printPixelRGB(k);
				int y  = (int)( 0.299   * temp[0] + 0.587   * temp[1] + 0.114   * temp[2]);
//				int cb = (int)(-0.16874 * r - 0.33126 * g + 0.50000 * b);
//				int cr = (int)( 0.50000 * r - 0.41869 * g - 0.08131 * b);
				channelY[i][j]=y;
//				System.out.print(y+" ");
			}
//			System.out.println();
		}
		return channelY;
	}

	public static int[] printPixelRGB(int rgb) {
		int[] r = new int[3];
		r[0] = (rgb >> 16) & 0xFF;
		r[1] = (rgb >> 8) & 0xFF;
		r[2] = rgb & 0xFF;
		return r;
	}
}
